<template>
    <div id="all">
        <nav>
            <span @touchstart="back">取消</span>
            <span>发布商品</span>
            <span>卖出技巧</span>
        </nav>
        <div id="box">
          <div id="img-list">
            <label for="image" ref="label" id="file-label">+</label>
            <input type="file" id="image" accept="image/jpeg,image/png,image/gif" multiple @change="chooseImage($event)" ref="img"> 
          </div>
        </div>
        <div id="message">
            <label for="">名字</label>
            <input type="text" v-model="name">
            <br>
            <label for="">价格</label>
            <input type="text" v-model="price">
            <br>
            <label for="">位置</label>
            <input type="text" v-model="area">
            <br>
            <label for="">数量</label>
            <input type="text" v-model.number="num">
        </div>
        <div id="select">
            <label for="">类目</label>
            <select v-model="type">
                <option value="手机/配件">手机/配件</option>
                <option value="电脑/周边">电脑/周边</option>
                <option value="数码产品">数码产品</option>
                <option value="居家生活">居家生活</option>
                <option value="服饰">服饰</option>
                <option value="鞋履">鞋履</option>
                <option value="食品/保健品">食品/保健品</option>
                <option value="珠宝饰品">珠宝饰品</option>
                <option value="其他">其他</option>
            </select>
        </div>
        <button @touchstart="sent">发布</button>
    </div>
</template>

<script>
export default{
    data:function(){
        return{
            name:"",
            price:"",
            area:"",
            num:"",
            type:"手机/配件",
            files:[],
            img:[],
        }
    },
    methods:{
        back:function(){
            history.back();
        },
        chooseImage:function(e){
            var fileLabel =document.getElementById("file-label");
            var imgList =document.getElementById("img-list");
            for(var i = 0;i<e.target.files.length;i++){
            var f = e.target.files[i];
            this.files.push(f);
            var img =document.createElement("img");
            img.classList.add("images");
            img.style.width="28vw";
            img.style.height="28vw";
            img.style.marginTop="10px";
            img.style.marginRight="6px";
            var url = URL.createObjectURL(f);
            img.src = url;
            imgList.insertBefore(img,fileLabel);
            fileLabel.style.display = "none";
            }
        },
        sent:function(){
            if(this.name==""||this.price==""||this.area==""||this.num==""||this.type==""){
                layer.msg("请先完善商品信息");
                return;
            }
            var data = new FormData();
            data.append('name',this.name);
            data.append('area',this.area);
            data.append('price',this.price);
            data.append('num',this.num);
            data.append('type',this.type);
            for(var i = 0;i<this.files.length;i++){
                data.append('pic',this.files[i]);
            }
            this.$store.dispatch('requestGoodUpload', data)
            .then(res=>{
                layer.msg(res.data.msg);
                this.name = "";
                this.price = "";
                this.area = "";
                this.num = "";
            })
        }
    }
}
</script>

<style scoped>
    #all{
        background-color: white;
    }
    nav{
        height: 40px;
        line-height: 40px;
        padding: 0 8px;
        display: flex;
        justify-content: space-between;
        border-bottom: 1px solid #ddd;
    }
    #box{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    }

    #img-list input{
        display: none;
    }
    #img-list{
        margin-top: 10px;
        margin-left: 20px;
    }
    #img-list label{
        width: 28vw;
        height: 28vw;
        border: solid 1px black;
        display: inline-block;
        font-size: 60px;
        text-align: center;
        line-height: 80px;
        vertical-align: top;
        margin-top: 10px;
        border-radius: 50%;
    }
    #message{
        width: 100%;
        margin-top: 40px;
        text-align: center;
    }
    #message input{
        border: none;
        border-bottom: 1px solid orangered;
        width: 50%;
        margin-bottom: 25px;
        font-size: 17px;
    }
    #select{
        margin-left: 66px;
    }
    button{
        width: 100%;
        height: 40px;
        border: none;
        background-color: red;
        color: white;
        font-size: 16px;
        position: absolute;
        left: 0;
        bottom: 0;
    }
</style>
